package com.gowants.pems.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * @Description
 * @Project aj-parent
 * @PageName  net.sinorock.aj.common.config
 * @ClassName CorsConfig
 * @author MengyuWu
 * @date 2019-8-229:59
  */
@Configuration
public class CorsConfig {
	@Autowired
    private FangshuaInterceptor interceptor;
	
   @Bean
   public WebMvcConfigurer CORSConfigurer() {
       return new WebMvcConfigurerAdapter() {
           @Override
           public void addCorsMappings(CorsRegistry registry) {
               registry.addMapping("/**")
                       .allowedOrigins("*")
                       .allowedMethods("*")
                       .allowedHeaders("*")
                       //设置是否允许跨域传cookie
                       .allowCredentials(true)
                       //设置缓存时间，减少重复响应
                       .maxAge(3600);
           }
           
           @Override
			public void addResourceHandlers(ResourceHandlerRegistry registry) {
				registry.addResourceHandler("/res/**").addResourceLocations("classpath:/res/");
				
			}
           @Override
           public void addInterceptors(InterceptorRegistry registry) {
               registry.addInterceptor(interceptor);
           }
       };
   }


   @Bean
   public FilterRegistrationBean corsFilter() {
       final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
       final CorsConfiguration config = new CorsConfiguration();
       // 允许cookies跨域
       config.setAllowCredentials(true);
       // #允许向该服务器提交请求的URI，*表示全部允许，在SpringMVC中，如果设成*，会自动转成当前请求头中的Origin
       config.addAllowedOrigin("*");
       // #允许访问的头信息,*表示全部
       config.addAllowedHeader("*");
       // 预检请求的缓存时间（秒），即在这个时间段里，对于相同的跨域请求不会再预检了
       config.setMaxAge(3600L);
       // 允许提交请求的方法，*表示全部允许
       config.addAllowedMethod("OPTIONS");
       config.addAllowedMethod("HEAD");
       config.addAllowedMethod("GET");
       config.addAllowedMethod("PUT");
       config.addAllowedMethod("POST");
       config.addAllowedMethod("DELETE");
       config.addAllowedMethod("PATCH");
       source.registerCorsConfiguration("/**", config);

       FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));
       // 设置监听器的优先级
       bean.setOrder(0);

       return bean;
   }
}
